import selenium.webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import unittest,time

import GreaterWMSyaml


def chrome_driver():
    driver = webdriver.Chrome()
    driver.implicitly_wait(10)
    driver.maximize_window()
    return driver
class GreaterWMSInstellTest(unittest.TestCase):
    cfg = GreaterWMSyaml.ReadYaml.read_yaml()
    def __init__(self, driver, url=cfg['url']):
        self.driver = driver
        self.url = url
        self.wait = WebDriverWait(driver=self.driver, timeout=5, poll_frequency=1.5)
        self.driver.get(url)

    def loc_wait(self, loc):
        return self.wait.until(EC.presence_of_element_located(loc))

    def find_ele(self, loc):
        self.loc_wait(loc)
        return self.driver.find_element(*(loc))

    def find_eles(self, loc):
        self.loc_wait(loc)
        return self.driver.find_elements(*(loc))

    def set_language(self):
        self.find_ele((By.XPATH, '//i[text()="translate"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//div[text()="中文简体"]/parent::div')).click()
        time.sleep(5)

    def login(self, username='1', passwrod='1'):
        self.find_ele((By.XPATH, '//span[text()="登入"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//span[text()="管理员登入"]/ancestor::button')).click()
        self.find_ele((By.XPATH, '//input[@aria-label="管理员"]')).send_keys(username)
        self.find_ele((By.XPATH, '//input[@aria-label="密码"]')).send_keys(passwrod)
        self.find_ele((By.XPATH, '//div[contains(@class,"justify-start")]//'
                                 'span[text()="登入"]/ancestor::button')).click()
        time.sleep(1)

    def edit_goods_weight(self,goodname=cfg['goods']['goodsname'],goodweight=cfg['goods(update)']['goodweight']):
        self.find_ele((By.XPATH, '//div[contains(text(),"商品管理")]/parent::a')).click()
        time.sleep(5)

        self.find_ele((By.XPATH,'//td[contains(text(),"{}")]/parent::tr//i[text()="edit"]'.format(goodname))).click()
        self.find_ele((By.XPATH, '//input[@aria-label="商品高度(单位:毫米)"]')).send_keys(Keys.COMMAND,'a')
        self.find_ele((By.XPATH, '//input[@aria-label="商品高度(单位:毫米)"]')).send_keys(goodweight)
        time.sleep(10)
        self.find_ele((By.XPATH,'//*[text()="check"]')).click()
        time.sleep(50)


    def edit_goods_lenght(self):
        pass



class TestInitGreaterWMS(unittest.TestCase):

    def setUp(self):
        self.driver = chrome_driver()

    def test_01(self):
        page = GreaterWMSInstellTest(self.driver)
        page.set_language()
        page.login()
        page.edit_goods_weight()
        time.sleep(5)

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()